package modes;

import gfx.Tickable;
import gfx.tiles.Tile;
import gfx.tiles.TileRenderer;

import java.util.ArrayList;
import java.util.Random;

import objects.Flag;
import objects.Robot;
import core.Main;
import core.Vector2;

public class Mode implements Tickable {

	private Vector2[] spawns;
	private ArrayList<Robot> robots = new ArrayList<Robot>();
	private int[] teams;
	private Vector2 goal;
	private Modes mode;
	public enum Modes {
		FREE_ROAM, CAPTURE_THE_FLAG, FIND_THE_PATH
	}
	
	public Mode(int mode) {
		switch (mode) {
			case 0:
				this.mode = Modes.FREE_ROAM;
				break;
			case 1:
				this.mode = Modes.CAPTURE_THE_FLAG;
				break;
			case 2:
				this.mode = Modes.FIND_THE_PATH;
				break;
		}
		setMode(this.mode);
	}
	
	@Override
	public void tick() {
		switch (mode) {
			case FREE_ROAM:
				for (int i = 0; i <  robots.size(); i++) {
					robotSelection(robots.get(i));
				}
				break;
			case CAPTURE_THE_FLAG:
				for (int i = 0; i <  robots.size(); i++) {
					Robot robot = robots.get(i);
					if (robot.getTeam() == -1) {
						if (teams[0] <= teams[1]) {
							teams[0]++;
							robot.setTeam(0);
							robot.setSpawn(spawns[0]);
						} else {
							teams[1]++;
							robot.setTeam(1);
							robot.setSpawn(spawns[1]);
						}
					}
					robotSelection(robot);
				}
				break;
			case FIND_THE_PATH:
				for (int i = 0; i < robots.size(); i++) {
					Robot robot = robots.get(i);
					if (robot.getTeam() == -1) {
						robot.setTeam(0);
						Random random = new Random();
						Vector2 tmp = new Vector2(random.nextInt((Main.worldWidth * Tile.size) / 2) + 
													(Main.worldWidth * Tile.size) / 2, 
													random.nextInt((Main.worldHeight * Tile.size) / 2) + 
													(Main.worldHeight * Tile.size) / 2);
		    			while(TileRenderer.tileCollision(tmp, Robot.SIZE) != Tile.AIR) {
		    				tmp = new Vector2(random.nextInt((Main.worldWidth * Tile.size) / 2) + 
												(Main.worldWidth * Tile.size) / 2, 
												random.nextInt((Main.worldHeight * Tile.size) / 2) + 
												(Main.worldHeight * Tile.size) / 2);
		    			}
		    			robot.setSpawn(tmp);
					}
					if (robot.getPos().distance(goal) < 50) {
						System.out.println("The winner is: " + robot.getName());
						mode = Modes.FREE_ROAM;
					}
					robotSelection(robot);
				}
				break;
		}
	}
	
	private void robotSelection(Robot robot) {
		if (robot.getID() == Main.selectedRobot && Main.selectedRobot != -1) {
			Main.mapPos = new Vector2(robot.getPos().x - Main.WIDTH / 2,
									robot.getPos().y - Main.HEIGHT / 2);
		}
	}
	
	public void setMode(Modes mode) {
		for (int i = 0; i <  robots.size(); i++) {
			robots.get(i).setTeam(-1);;
		}

		switch (mode) {
//			case FREE_ROAM:
//				break;
			case CAPTURE_THE_FLAG:
				spawns = new Vector2[] {new Vector2(100, 100), 
						new Vector2(Main.worldHeight * Tile.size - 100, 
									Main.worldHeight * Tile.size - 100)};
				teams = new int[] {0, 0};
				break;
			case FIND_THE_PATH:
				goal = new Vector2((Main.worldWidth * Tile.size) - 50, 50);
//				Flag flag = new Flag(goal, 0, new Vector2(60, 60));
//				Main.addDrawable(flag);
//				Main.addTickable(flag);
//				System.out.println("test");
//				Mine mine = new Mine(goal);
//				Main.addDrawable(mine);
//				Main.addTickable(mine);
				break;
			default:
				break;
		}
	}
	
	public Modes getMode() {
		return mode;
	}
	
	public void addRobot(Robot robot) {
		robots.add(robot);
	}
	
	public Vector2[] getSpawns() {
		return spawns;
	}

	public void setSpawns(Vector2[] spawns) {
		this.spawns = spawns;
	}
	
	public Vector2 getGoal() {
		return goal;
	}
	
	public void setGoal(Vector2 goal) {
		this.goal = goal;
	}

	@Override
	public Vector2 getPos() {
		return null;
	}

	@Override
	public Vector2 getSize() {
		return null;
	}



	@Override
	public boolean removed() {
		return false;
	}

	@Override
	public int getID() {
		return -1;
	}
	
}
